
/**


 * Copyright (c) 2010,  Joel Andersson, Olle Br�nnlund, Hannes Gustafsson All rights reserved.

 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:

 *          - Redistributions of source code must retain the above copyright
 *          notice, this list of conditions and the following disclaimer.
 *          - Redistributions in binary form must reproduce the above copyright
 *          notice, this list of conditions and the following disclaimer in the
 *          documentation and/or other materials provided with the
 *          distribution.
 *          - Neither the name of the owner nor the names of its contributors
 *          may be used to endorse or promote products derived from this
 *          software without specific prior written permission.

 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class RectangleSpace extends JFrame {

    private ArrayList<Point> ps;
    private ArrayList<Rectangle> emptyRecs;
    private char key = '1';
    private static final int NO_CLICK = -1;
    private int mouseClick = NO_CLICK;
    private static final int WIDTH_ = 400, HEIGHT_ = 300;
    private int xMaX, yMax;

    public RectangleSpace(String name, int xMaX, int yMax, ArrayList<Point> ps,
            ArrayList<Rectangle> emptyRecs) {
        super(name);
        this.xMaX = xMaX;
        this.yMax = yMax;
        assert emptyRecs != null;
        this.ps = ps;
        this.emptyRecs = emptyRecs;
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.setContentPane(new DrawingBoard());
        this.pack();
        this.setLocationByPlatform(true);
        this.addMouseListener(new MouseListener() {

            public void mouseReleased(MouseEvent e) {

                mouseClick = e.getButton();
                repaint();
            }

            public void mousePressed(MouseEvent e) {
            }

            public void mouseExited(MouseEvent e) {
            }

            public void mouseEntered(MouseEvent e) {
            }

            public void mouseClicked(MouseEvent e) {
            }
        });
        this.addKeyListener(new KeyListener() {

            public void keyPressed(KeyEvent e) {
                // TODO Auto-generated method stub
                key = e.getKeyChar();
                repaint();
            }

            public void keyReleased(KeyEvent e) {
            }

            public void keyTyped(KeyEvent e) {
            }
        });
        this.setVisible(true);
    }

    private class DrawingBoard extends JPanel {

        public DrawingBoard() {
            this.setPreferredSize(new Dimension(WIDTH_, HEIGHT_));

            this.setBackground(Color.WHITE);
        }

        private void keyStroke(Graphics2D g2D) {
            int index = 10;
            if (Character.isDigit(key)) {
                index = Integer.parseInt(key + "");
            }
            g2D.setColor(Color.blue);
            if (index >= 0 && index <= 9 && index < emptyRecs.size()) {
                Rectangle r = emptyRecs.get(index);
                g2D.draw(r);
                System.out.println(r.toString() + " Area: " + r.height * r.width);
            } else {
                for (Rectangle r : emptyRecs) {
                    g2D.draw(r);
                }
            }
        }

        private void mouseClick(Graphics2D g2D) {
            g2D.setColor(Color.blue);
            Rectangle r = emptyRecs.get(recNr);
            g2D.draw(r);
            System.out.println(r.toString() + " Area: " + r.height * r.width);
            if (mouseClick == MouseEvent.BUTTON1) {
                recNr = (recNr + 1) % emptyRecs.size();
            } else {
                if (recNr > 0) {
                    recNr--;
                } else {
                    recNr = emptyRecs.size() - 1;
                }
            }
            mouseClick = NO_CLICK;
        }
        private int recNr = 0;

        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2D = (Graphics2D) g;
            g2D.scale(this.getWidth() / (xMaX * 1f), this.getHeight() / (yMax * 1f));
            // System.out.println("I paintComp");
            g2D.setBackground(Color.red);
//            g2D.drawLine(0, HEIGHT_, WIDTH_, HEIGHT_);
//            g2D.drawLine(WIDTH_, HEIGHT_, WIDTH_, 0);
            g2D.setBackground(Color.white);
            g2D.setColor(Color.black);
            for (Point p : ps) {
                g2D.fillOval(p.x - 1, p.y - 1, 6, 6); //xMaX/WIDTH_ +1 , yMax/HEIGHT_ );
            }
            if (emptyRecs != null) {
                if (mouseClick == NO_CLICK) {
                    keyStroke(g2D);
                } else {
                    mouseClick(g2D);
                }
            }
        }
    }
}
